<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="流程id" prop="piid">
        <el-input
          v-model="queryParams.piid"
          placeholder="请输入流程id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工单号" prop="workId">
        <el-input
          v-model="queryParams.workId"
          placeholder="请输入工单号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请人id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入申请人uid"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入申请人姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="组织名称" prop="orgName">
        <el-input
          v-model="queryParams.orgName"
          placeholder="请输入申请人组织名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否删除" prop="deleted">
        <el-input
          v-model="queryParams.deleted"
          placeholder="请输入是否删除"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['biz:flowMainInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['biz:flowMainInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['biz:flowMainInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['biz:flowMainInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="flowMainInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="流程id" align="center" prop="piid" />
      <el-table-column label="工单号" align="center" prop="workId" />
      <el-table-column label="工单标题" align="center" prop="workName" />
      <el-table-column label="申请人uid" align="center" prop="userId" />
      <el-table-column label="申请人姓名" align="center" prop="userName" />
      <el-table-column label="申请人组织名称" align="center" prop="orgName" />
      <el-table-column label="流程状态" align="center" prop="state" />
      <el-table-column label="是否删除" align="center" prop="deleted" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['biz:flowMainInfo:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['biz:flowMainInfo:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改流程工单记录对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="flowMainInfoRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="流程id" prop="piid">
          <el-input v-model="form.piid" placeholder="请输入流程id" />
        </el-form-item>
        <el-form-item label="工单号" prop="workId">
          <el-input v-model="form.workId" placeholder="请输入工单号" />
        </el-form-item>
        <el-form-item label="工单标题" prop="workName">
          <el-input v-model="form.workName" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="上级流程id" prop="ppiid">
          <el-input v-model="form.ppiid" placeholder="请输入上级流程id" />
        </el-form-item>
        <el-form-item label="申请人uid" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入申请人uid" />
        </el-form-item>
        <el-form-item label="申请人姓名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入申请人姓名" />
        </el-form-item>
        <el-form-item label="申请人员工号" prop="userIdCard">
          <el-input v-model="form.userIdCard" placeholder="请输入申请人员工号" />
        </el-form-item>
        <el-form-item label="申请人组织dn" prop="orgDn">
          <el-input v-model="form.orgDn" placeholder="请输入申请人组织dn" />
        </el-form-item>
        <el-form-item label="申请人组织名称" prop="orgName">
          <el-input v-model="form.orgName" placeholder="请输入申请人组织名称" />
        </el-form-item>
        <el-form-item label="申请人电话" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入申请人电话" />
        </el-form-item>
        <el-form-item label="申请人邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入申请人邮箱" />
        </el-form-item>
        <el-form-item label="流程模版编号" prop="templateId">
          <el-input v-model="form.templateId" placeholder="请输入流程模版编号" />
        </el-form-item>
        <el-form-item label="流程状态" prop="state">
          <el-input v-model="form.state" placeholder="请输入流程状态" />
        </el-form-item>
        <el-form-item label="流程业务信息" prop="bizInfo">
          <el-input v-model="form.bizInfo" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="Moa字段列表" prop="moaShowName">
          <el-input v-model="form.moaShowName" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="工单描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="公司名称" prop="company">
          <el-input v-model="form.company" placeholder="请输入公司名称" />
        </el-form-item>
        <el-form-item label="节点名称" prop="nodeName">
          <el-input v-model="form.nodeName" placeholder="请输入节点名称" />
        </el-form-item>
        <el-form-item label="处理人Id" prop="ownerId">
          <el-input v-model="form.ownerId" placeholder="请输入处理人Id" />
        </el-form-item>
        <el-form-item label="处理人姓名" prop="ownerName">
          <el-input v-model="form.ownerName" placeholder="请输入处理人姓名" />
        </el-form-item>
        <el-form-item label="是否删除" prop="deleted">
          <el-input v-model="form.deleted" placeholder="请输入是否删除" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { listFlowMainInfo, getFlowMainInfo, delFlowMainInfo, addFlowMainInfo, updateFlowMainInfo } from "@/api/biz/flowMainInfo";

const { proxy } = getCurrentInstance();

const flowMainInfoList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    piid: null,
    workId: null,
    workName: null,
    userId: null,
    userName: null,
    orgName: null,
    deleted: null,
  },
  rules: {
    piid: [{ required: true, message: "流程id不能为空", trigger: "blur" }],
    workId: [{ required: true, message: "工单号不能为空", trigger: "blur" }],
    workName: [{ required: true, message: "工单标题不能为空", trigger: "blur" }],
    userId: [{ required: true, message: "申请人uid不能为空", trigger: "blur" }],
    userName: [{ required: true, message: "申请人姓名不能为空", trigger: "blur" }],
    orgDn: [{ required: true, message: "申请人组织dn不能为空", trigger: "blur" }],
    orgName: [{ required: true, message: "申请人组织名称不能为空", trigger: "blur" }],
    templateId: [{ required: true, message: "流程模版编号不能为空", trigger: "blur" }],
    state: [{ required: true, message: "流程状态不能为空", trigger: "blur" }],
    createTime: [{ required: true, message: "创建时间不能为空", trigger: "blur" }],
    updateTime: [{ required: true, message: "修改时间不能为空", trigger: "blur" }],
    deleted: [{ required: true, message: "是否删除不能为空", trigger: "blur" }],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询流程工单记录列表 */
async function getList() {
  loading.value = true;
  const response  = await listFlowMainInfo(queryParams.value);
  flowMainInfoList.value = response.rows;
  total.value = response.total;
  loading.value = false;
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    piid: null,
    workId: null,
    workName: null,
    ppiid: null,
    userId: null,
    userName: null,
    userIdCard: null,
    orgDn: null,
    orgName: null,
    mobile: null,
    email: null,
    templateId: null,
    state: null,
    bizInfo: null,
    moaShowName: null,
    description: null,
    createTime: null,
    updateTime: null,
    nodeName: null,
    ownerId: null,
    ownerName: null,
    company: null,
    deleted: null
  };
  proxy.resetForm("flowMainInfoRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加流程工单记录";
}

/** 修改按钮操作 */
async function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  const response = await getFlowMainInfo(_id);
  form.value = response.data;
  open.value = true;
  title.value = "修改流程工单记录";
}

/** 提交按钮 */
async function submitForm() {
  try {
    const valid = await proxy.$refs["flowMainInfoRef"].validate();
    if (valid) {
      if (form.value.id != null) {
        await updateFlowMainInfo(form.value);
        proxy.$modal.msgSuccess("修改成功");
      } else {
        await addFlowMainInfo(form.value);
        proxy.$modal.msgSuccess("新增成功");
      }
      open.value = false;
      getList();
    }
  } catch (error) {
    // 校验失败界面自动有红色标记，可以不需要提示； 接口调用失败有接口本身的错误提示
  }
}

/** 删除按钮操作 */
async function handleDelete(row) {
  try {
    const _ids = row.id || ids.value;
    await proxy.$modal.confirm('是否确认删除流程工单记录编号为"' + _ids + '"的数据项？');
    await delFlowMainInfo(_ids);
    getList();
    proxy.$modal.msgSuccess("删除成功");
  } catch (error) {
    // 用户取消会走到这里，不需要提示； 接口调用失败有接口本身的错误提示
  }
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('/biz/flowMainInfo/export', {
    ...queryParams.value
  }, `flowMainInfo_${new Date().getTime()}.xlsx`)
}

getList();
</script>
